#region License

/*
  GShield - Game Shield
  Copyright (C) 2006-2007 WaveTech Digital Technologies, Inc.
                                http://www.wtdt.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#endregion License

#region Includes

using System;
using System.Drawing;
using System.Windows.Forms;
using WaveTech.GShield.Data;
using WaveTech.GShield.Engine.Factories;
using WaveTech.GShield.Engine.Systems;
using WaveTech.GShield.Engine.Tools;
using WaveTech.GShield.Manager.Properties;
using WaveTech.GShield.Model;

#endregion

namespace WaveTech.GShield.Manager
{
	public partial class BannedGames : Form
	{
		#region Constructor

		public BannedGames()
		{
			InitializeComponent();
			InitializeRequiredSystems();
		}

		#endregion Constructor

		#region Initialize Required Systems

		private void InitializeRequiredSystems()
		{
		}

		#endregion Initialize Required Systems

		#region BannedGames_Load

		private void BannedGames_Load(object sender, EventArgs e)
		{
			m_bannerImage.Image = BannerFactory.CreateBanner(m_bannerImage.Width,
			                                                 m_bannerImage.Height, BannerFactory.BannerStyle.WinVistaBlack,
			                                                 new Bitmap(Resources.B256x256_BannedGames, new Size(48, 48)),
			                                                 "Banned Games Policies",
			                                                 "Specific Game Banning By User");

			PopulateUsersLists();
			PopulateGamesList();
		}

		#endregion BannedGames_Load

		#region Populate Users Lists

		private void PopulateUsersLists()
		{
			cboBannedGamesUsersList.Items.Clear();
			cboBannedGamesUsersList.Items.AddRange(Users.GetUsersList());
		}

		#endregion Populate Users Lists

		#region Populate Games List

		private void PopulateGamesList()
		{
			lstBannedGames.DataSource = GamesDAO.GetGameList();
		}

		#endregion Populate Games List

		#region btnRemoveUser_Click

		private void btnRemoveUser_Click(object sender, EventArgs e)
		{
			if (lstBannedGames.SelectedItem != null && lstBannedGames.SelectedItem.ToString().Trim().Length > 0)
			{
				if (lstBannedGameUsers.SelectedItem != null && lstBannedGameUsers.SelectedItem.ToString().Trim().Length > 0)
				{
					if (
						MessageBox.Show(
							"Are you sure you want to unban " + lstBannedGameUsers.SelectedItem + " from " + lstBannedGames.SelectedItem +
							"?", "Un-Ban Game", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
					{
						Cursor.Current = Cursors.WaitCursor;

						Game g = GamesDAO.GetGameByName(lstBannedGames.SelectedItem.ToString());

						if (BannedDAO.IsGameBanned(g.Id, lstBannedGameUsers.SelectedItem.ToString()))
						{
							string user = lstBannedGameUsers.SelectedItem.ToString();

							BannedPolicy p = BannedDAO.GetBannedPolicyByGameUser(g.Id, lstBannedGameUsers.SelectedItem.ToString());
							BannedDAO.Delete(p.Id);

							Logging.Write(
								"The user " + lstBannedGameUsers.SelectedItem + " has been unbanned from the game " + g.Name + ".",
								LogCategory.Banned, LogPriority.Medium, LogType.BannedPolicyDel);

							lstBannedGameUsers.DataSource = BannedDAO.GetUsersListForGame(g.Id);

							Cursor.Current = Cursors.Default;

							MessageBox.Show("User " + user + " successfully un-banned from game " + lstBannedGames.SelectedItem + ".",
							                "Ban From Game", MessageBoxButtons.OK, MessageBoxIcon.Information);
						}
						else
						{
							Cursor.Current = Cursors.Default;

							MessageBox.Show(
								"The selected user, " + lstBannedGameUsers.SelectedItem + ", is not banned for the game " +
								lstBannedGames.SelectedItem + ".", "Failed to UnBan Game", MessageBoxButtons.OK, MessageBoxIcon.Error);
						}
					}
				}
				else
				{
					Cursor.Current = Cursors.Default;

					MessageBox.Show("You must select a user to un-ban from a game.", "Banned Games", MessageBoxButtons.OK,
					                MessageBoxIcon.Error);
				}
			}
			else
			{
				Cursor.Current = Cursors.Default;

				MessageBox.Show("You must select a game from the list to un-ban a user from it.", "Banned Games",
				                MessageBoxButtons.OK,
				                MessageBoxIcon.Error);
			}

			Cursor.Current = Cursors.Default;
		}

		#endregion btnRemoveUser_Click

		#region lstBannedGames_SelectedIndexChanged

		private void lstBannedGames_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (lstBannedGames.SelectedItem != null && lstBannedGames.SelectedItem.ToString().Trim().Length > 0)
			{
				Cursor.Current = Cursors.WaitCursor;

				Game g = GamesDAO.GetGameByName(lstBannedGames.SelectedItem.ToString());
				lstBannedGameUsers.DataSource = BannedDAO.GetUsersListForGame(g.Id);

				cboBannedGamesUsersList.SelectedIndex = -1;

				Cursor.Current = Cursors.Default;
			}
			else
			{
				cboBannedGamesUsersList.SelectedIndex = -1;
				lstBannedGameUsers.Items.Clear();
			}

			Cursor.Current = Cursors.Default;
		}

		#endregion lstBannedGames_SelectedIndexChanged

		#region btnSave_Click

		private void btnSave_Click(object sender, EventArgs e)
		{
			if (lstBannedGames.SelectedItem != null && lstBannedGames.SelectedItem.ToString().Trim().Length > 0)
			{
				if (cboBannedGamesUsersList.SelectedItem != null &&
				    cboBannedGamesUsersList.SelectedItem.ToString().Trim().Length > 0)
				{
					if (
						MessageBox.Show(
							"Are you sure you want to ban " + cboBannedGamesUsersList.SelectedItem + " from playing the game " +
							lstBannedGames.SelectedItem + "?", "Ban Game", MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
						DialogResult.Yes)
					{
						Cursor.Current = Cursors.WaitCursor;

						Game g = GamesDAO.GetGameByName(lstBannedGames.SelectedItem.ToString());

						if (BannedDAO.IsGameBanned(g.Id, cboBannedGamesUsersList.SelectedItem.ToString()))
						{
							Cursor.Current = Cursors.Default;

							MessageBox.Show(
								"The selected user, " + cboBannedGamesUsersList.SelectedItem + ", is already banned for the game " +
								lstBannedGames.SelectedItem + ".", "Failed to Ban Game", MessageBoxButtons.OK, MessageBoxIcon.Error);
						}
						else
						{
							BannedPolicy p = new BannedPolicy();
							p.DateBanned = DateTime.Now;
							p.GameId = g.Id;
							p.Username = cboBannedGamesUsersList.SelectedItem.ToString();

							BannedDAO.Insert(p);

							Logging.Write(
								"The user " + cboBannedGamesUsersList.SelectedItem + " has been banned from the game " + g.Name + ".",
								LogCategory.Banned, LogPriority.Medium, LogType.BannedPolicyAdd);

							g = GamesDAO.GetGameByName(lstBannedGames.SelectedItem.ToString());
							lstBannedGameUsers.DataSource = BannedDAO.GetUsersListForGame(g.Id);

							Cursor.Current = Cursors.Default;

							MessageBox.Show("User " + p.Username + " successfully banned from game " + lstBannedGames.SelectedItem + ".",
							                "Ban From Game", MessageBoxButtons.OK, MessageBoxIcon.Information);
						}
					}
				}
				else
				{
					Cursor.Current = Cursors.Default;

					MessageBox.Show("You must select a user to ban from a game.", "Banned Games", MessageBoxButtons.OK,
					                MessageBoxIcon.Error);
				}
			}
			else
			{
				Cursor.Current = Cursors.Default;

				MessageBox.Show("You must select a game from the list to ban users from it.", "Banned Games", MessageBoxButtons.OK,
				                MessageBoxIcon.Error);
			}

			Cursor.Current = Cursors.Default;
		}

		#endregion btnSave_Click

		#region btnClose_Click

		private void btnClose_Click(object sender, EventArgs e)
		{
			Close();
		}

		#endregion btnClose_Click

		#region btnHelp_Click

		private void btnHelp_Click(object sender, EventArgs e)
		{
			Help.ShowHelp(this, Settings.Default.HelpFileName, HelpNavigator.TopicId, Settings.Default.HelpTopics_BannedGamesForm);
		}

		#endregion btnHelp_Click
	}
}